<?php

namespace app\api\controller\supplier;

use app\api\base\RestfulBase;
use app\modules\supplier\logic\SupplierUserLogic;
use app\modules\supplier\call\Auth as SupplierAuthCall;

Class SupplierUser extends RestfulBase
{
    protected $logic;
    protected $spec_action =['update'];//特殊方法

    protected function _initialize()
    {

        if (  in_array($this->this_action,$this->spec_action ) ){
            $type = $this->getParams('type','','strip_tags');
            if ($type === 'forget'){ //忘记密码排除认证
                $this->_open_auth = ['user' => false, 'open_api' => false];
            }
        }
        $this->logic = new SupplierUserLogic();
    }

    /**
     * 获取供应商用户信息
     * @return Response
     */
    public function read()
    {
        if ($this->request->isGet()){
            $id = !empty($this->getParams('supplier_user_id', '', 'trim,strip_tags'))?$this->getParams('supplier_user_id', '', 'trim,strip_tags'):$this->_account['supplier_user_id'];
            $data = $this->logic->get($id);
            if (is_array($data)) {
                return $this->apiSuccess($data);
            } else {
                return $this->apiError($this->logic->getError());
            }
        }else{
            return $this->apiError('无效请求');
        }

    }

    /**
     * 供应商信息|密码|忘记密码
     * @return Response
     */
    public function update(){
        if ($this->request->isPost()){
            $id = !empty($this->getParams('supplier_user_id','','trim,strip_tags'))?$this->getParams('supplier_user_id','','trim,strip_tags'):$this->_account['supplier_user_id'];
            $type = $this->getParams('type','','trim,strip_tags');
            if (empty($type)){
                return $this->apiError('Update 请求无效');
            }

            switch ($type){
                case 'info': //修改信息
                    return $this->_update_info($id);
                    break;
                case 'password'://修改密码
                    return $this->_update_password($id);
                    break;
                case 'forget'://忘记密码
                    return $this->_update_forget();
                    break;

            }

        }else{
            return $this->apiError('无效请求');
        }
    }

    /**
     * 供应商信息修改
     * @return Response
     */
    protected function _update_info($id = null){
           $data['username'] = $this->getParams('username','','trim,strip_tags');

           if ($this->logic->edit_info($id,$data)){
               return $this->apiSuccess();
           }else{
               return $this->apiError($this->logic->getError());
           }
    }

    protected function _update_password($id =null){
           $data['old_password'] = $this->getParams('old_password','','trim,strip_tags');
           $data['password'] = $this->getParams('password','','trim,strip_tags');
           $data['repeat_password'] = $this->getParams('repeat_password','','trim,strip_tags');

           if ($this->logic->edit_password($id,$data)){
                //强制注销登录状态
                if(true===SupplierAuthCall::logout()){
                    return $this->apiSuccess();
                }else{
                    return $this->apiError(SupplierAuthCall::getError(";"));
                }
           }else{
               return $this->apiError($this->logic->getError());
           }
    }

    /**
     * 忘记密码 找回方式 短信找回
     * @return Response
     */
    protected function _update_forget(){
        $username = $this->getParams('username','','trim,strip_tags');

        if ($this->logic->edit_forget($username)){

            return $this->apiSuccess();

        }else{
            return $this->apiError($this->logic->getError());
        }
    }


}

